package com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.adhoc;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.BitAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @author lt
 * @version 1.0
 * @date 2021-04-18 19:46:53
 * @email okprog@sina.com
 */
public class BitAutoHaoArticleSimpleCrawlerSchedulerJob extends BitAutoCommonCrawlerSchedulerJob {
    private static final String crawler_level = "simple";

    public static void main(String[] args) {
        publishBitAutoCrawlerSchedulerJobInfo();
    }
    public static CrawlerJob publishBitAutoCrawlerSchedulerJobInfo(){
        CrawlerJob bitAutoScheduler = bitAutoScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(bitAutoScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("易车号新闻回溯回复采集任务");
        crawlerScheduleJob.setCrawlerKey(bitAutoScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return bitAutoScheduler;
    }

    public static CrawlerJob bitAutoScheduler(){

        //易车号-追溯回复采集
        CrawlerRequestRecord haoArticleCrawlerComment = doHaoArticleCommentTraceCrawler();
        CrawlerRequestRecord initCommentRecord = initCommentRecord(haoArticleCrawlerComment);
        String site = haoArticleCrawlerComment.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");
        //request队列名
        String requestQueueName = StringUtils.joinWith("-","crawler",domain, site, crawler_level,"queue");

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline("kafka_result",kafkaTopciForTraceJob,null)
                .crawlerJobThreadNumber(35)
                .requestRecord(haoArticleCrawlerComment)
                .supportRecord(initCommentRecord)
                .build();
    }

    public static CrawlerRequestRecord initCommentRecord(CrawlerRequestRecord startRequestRecord){
        CrawlerRequestRecord initRecord = CrawlerRequestRecord.builder()
                .itemPageRequest(startRequestRecord)
                .httpUrl("https://hao.yiche.com/")
                .recordKey("https://hao.yiche.com/")
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .copyBizTags()
                .build();
        String site = startRequestRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");
        String requestQueueName = StringUtils.joinWith("-","crawler",domain, site, crawler_level,"queue");
        CrawlerRequestRecord initCommentCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("bitauto_hao_article_comment",turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/crawler/domain/common/api/v1/bitauto/results/queue/init?requestQueueName="
                        + requestQueueName + "&site=" + site + "&hourFromNow=75")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(initRecord), "utf-8");
        initCommentCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initCommentCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);
        return initCommentCrawlerRecord;
    }


    public static CrawlerRequestRecord doHaoArticleCommentTraceCrawler(){
        String site = "hao_article";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .recordKey("https://hao.yiche.com/#comment")
                .httpUrl("https://hao.yiche.com/#comment")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .domain(domain)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*4,null))
                .proxy(proxy)
                .build();

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        return requestRecord;
    }
}
